package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemMapper checkItemMapper;

    // 添加检查项
    public void add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
    }

//     检查项分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        int currentPage = queryPageBean.getCurrentPage();
        int pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> page = checkItemMapper.findPage(queryPageBean);
        return new PageResult(page.getTotal(), page.getResult());
    }

    // 根据id删除检查项
    public void deleteByid(Integer id) {
//判断是否有检查组关联检查项
        int count = checkItemMapper.findCountByCheckItemId(id);
        if (count > 0) {
            throw new RuntimeException("当前检查项被引用，不能删除");
        }
        checkItemMapper.deleteByid(id);
    }

//     编辑检查项
    public void edit(CheckItem checkItem) {
        checkItemMapper.edit(checkItem);
    }

    // 根据id查询检查项
    public CheckItem findById(Integer id) {
        return checkItemMapper.findById(id);
    }
}
